package cn.afactory.tygz.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nostra13.universalimageloader.utils.ImageLoaderUtil;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.UploadFileInfo;
import cn.afactory.tygz.ui.base.utils.ScreenUtil;
import cn.afactory.tygz.ui.base.utils.sns.PhotoData;
import cn.afactory.tygz.ui.fragments.SecondGoodsAddFragment;

/**
 * Created by Kenneth on 15/9/27.
 */
public class SecondGoodsAddImgGridAdapter extends BaseAdapter {
    private LayoutInflater inflater; // 视图容器
    private int selectedPosition = -1;// 选中的位置
    private boolean shape;
    private Context mContext;
    private List<PhotoData> mData = new ArrayList<PhotoData>();
    private int mIamgeWidth = 0, mIamgeHeight = 0;

    public SecondGoodsAddImgGridAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        mContext = context;
        mIamgeWidth = (ScreenUtil.getScreenWidth(mContext) - mContext.getResources().getDimensionPixelOffset(R.dimen.healthcircle_padding10) * 2)* 4 / 5 / 3;
        mIamgeHeight = mIamgeWidth;
    }

    @Override
    public int getCount() {
        if(mData.size() == SecondGoodsAddFragment.MAX_COUNT){
            return mData.size();
        }else {
            return (mData.size() + 1);
        }
    }

    @Override
    public Object getItem(int position) {
        return position > mData.size() ? null : mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        convertView = inflater.inflate(R.layout.item_grid_image,parent, false);
        holder = new ViewHolder();
        holder.image = (ImageView) convertView.findViewById(R.id.grid_image);
        if(mData.size() == SecondGoodsAddFragment.MAX_COUNT){
            holder.image.setVisibility(View.VISIBLE);
            ImageLoaderUtil.loadLocalImage(holder.image, !TextUtils.isEmpty(mData.get(position).mThumbnail) ? mData.get(position).mThumbnail : mData.get(position).mLocalUrl);
        }else {
            if (position == mData.size() + 1 || position == mData.size()) {
                holder.image.setVisibility(View.VISIBLE);
//            ImageLoaderUtil.loadLocalImage(holder.image, mData.get(position).mLocalUrl);
//            holder.image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.hc_button_add));
                holder.image.setImageResource(R.drawable.hc_button_add);
                if (position == SecondGoodsAddFragment.MAX_COUNT) {
                    holder.image.setVisibility(View.GONE);
                }
            } else {
                holder.image.setVisibility(View.VISIBLE);
                ImageLoaderUtil.loadLocalImage(holder.image, !TextUtils.isEmpty(mData.get(position).mThumbnail) ? mData.get(position).mThumbnail : mData.get(position).mLocalUrl);
            }
        }
        return convertView;
    }

    public void setData(List<PhotoData> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public class ViewHolder {
        public ImageView image;
    }
}
